package com.fhd.dao.Recharge;

import com.fhd.dao.BaseDao;
import com.fhd.pojo.Client;
import com.fhd.pojo.Recharge;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class RechargeDaoImplement implements RechargeDao{
    // 获得某个特定的充值对象
    @Override
    public Recharge getOneRecharge(Connection conn, int id) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Recharge recharge = new Recharge();

        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "select * from client where id = ?";
                Object[] parameters = {id};
                rs = BaseDao.executeQuery(conn, pstmt, rs, sql, parameters);

                if(rs.next()) {
                    recharge.setId(rs.getInt("id"));
                    recharge.setMoney(rs.getInt("money"));
                    recharge.setClientId(rs.getInt("client_id"));
                    recharge.setSituation(rs.getInt("situation"));
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,rs);
        }
        return recharge;
    }

    // 获取某个用户下的所有充值数据
    @Override
    public ArrayList<Recharge> getSomeoneRecharge(Connection conn,int clientId) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<Recharge> recharges = new ArrayList<>();

        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "select * from recharge where client_id = ?";
                Object[] parameters = {clientId};
                rs = BaseDao.executeQuery(conn, pstmt, rs, sql, parameters);

                while (rs.next()) {
                    Recharge recharge = new Recharge();
                    recharge.setId(rs.getInt("id"));
                    recharge.setMoney(rs.getInt("money"));
                    recharge.setClientId(rs.getInt("client_id"));
                    recharge.setSituation(rs.getInt("situation"));
                    recharges.add(recharge);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,rs);
        }
        return recharges;
    }

    // 获取所有的充值数据
    @Override
    public ArrayList<Recharge> getAllRecharge(Connection conn) {
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        ArrayList<Recharge> recharges = new ArrayList<>();

        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "select r.* , c.user_name from recharge r,client c where r.client_id = c.id";
                Object[] parameters = {};
                rs = BaseDao.executeQuery(conn, pstmt, rs, sql, parameters);

                while (rs.next()) {
                    Recharge recharge = new Recharge();
                    recharge.setId(rs.getInt("id"));
                    recharge.setMoney(rs.getInt("money"));
                    recharge.setClientId(rs.getInt("client_id"));
                    recharge.setSituation(rs.getInt("situation"));
                    recharge.setClientName(rs.getString("user_name"));
                    recharges.add(recharge);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,rs);
        }
        return recharges;
    }

    // 客户充值创建充值数据
    @Override
    public int clientRecharge(Connection conn, int clientId, int money) {
        PreparedStatement pstmt = null;
        int affectedRow = 0;
        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "insert into recharge (money, client_id) values (?,?)";
                Object[] parameters = {money,clientId};
                affectedRow = BaseDao.executeUpdate(conn, pstmt,sql, parameters);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,null);
        }
        return affectedRow;
    }

    // 改变充值状态
    @Override
    public int managerChangeRechargeSituation(Connection conn, int id, int situation) {
        PreparedStatement pstmt = null;
        int affectedRow = 0;
        try {
            if (conn != null) {
                // 编写sql语句,并把参数放进数组中
                String sql = "update recharge set situation = ? where id = ?";
                Object[] parameters = {situation,id};
                affectedRow = BaseDao.executeUpdate(conn, pstmt,sql, parameters);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(null,pstmt,null);
        }
        return affectedRow;
    }
}
